<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Model\Admin\Admin;
use App\Model\Device;
use App\Model\DevicePortErrLog;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use Phper666\JWTAuth\JWT;
use App\Controller\AbstractController;

/**
 * 设备端口错误日志
 */
class DevicePortErrLogController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;
    protected JWT $jwt;

    #[Inject]
    protected Admin $admin;

    public function __construct()
    {
        $this->jwt = container()->get(JWT::class);
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**
     * 列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getList(RequestInterface $request): ResponseInterface
    {
        $page = $request->inputs(['page', 'limit', 'keyword', 'status','distributor_id']);
        $page['info'] = $this->admin->getInfo($request);
        $model = DevicePortErrLog::query()
            ->where(function ($query) use ($page) {
                if ($page['distributor_id']) {
                    $devices = Device::where('distributor_id', $page['distributor_id'])->pluck('id');
                    $query->whereIn('device_id', $devices);
                } else {
                    if (isset($page['info']['distributor_id'])) {
                        $devices = Device::where('distributor_id', $page['info']['distributor_id'])->pluck('id');
                        $query->whereIn('device_id', $devices);
                    }
                }
            })
            ->where(function ($query) use ($page) {
                if (isset($page['keyword']) && $page['keyword'] !== '') {
                    $device_id = Device::where('sn', $page['keyword'])->value('id');
                    $query->where('device_id', $device_id);
                }
                if (isset($page['status']) && $page['status'] !== '') {
                    $query->where('status', $page['status']);
                }
            })->with('device');
        $data['total'] = $model->count();
        $data['list'] = $model
            ->orderBy('id', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->get()->toArray();
        return $this->success($data);
    }

    /**
     * 变更端口故障状态为已处理
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function editStatus(RequestInterface $request): ResponseInterface
    {
        try {
            $id = $request->input('id');
            $model = DevicePortErrLog::find($id);
            $model->status = DevicePortErrLog::STATUS['handle_yes'];
            $model->save();
            return $this->success();
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
